package com.group7.mall.controller;

import com.group7.mall.bean.Result;
import com.group7.mall.dao.TbMessageAuditFormDao;
import com.group7.mall.dao.TbSellerDao;
import com.group7.mall.dao.TbStoreAuditFormDao;
import com.group7.mall.entity.TbMessageAuditForm;
import com.group7.mall.entity.TbSeller;
import com.group7.mall.entity.TbStoreAuditForm;
import com.group7.mall.entity.TbUser;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@RequestMapping("seller-store")
@RestController
public class SellerStoreController {

    @Resource
    private TbSellerDao tbSellerDao;

    @Resource
    private TbMessageAuditFormDao tbMessageAuditFormDao;

    @PostMapping("show-store-info")
    public Result<TbSeller> showStoreInfo(@RequestBody TbUser tbUser) {
        return Result.ok(tbSellerDao.queryBySellerId(tbUser.getUserOpenId()));
    }

    @PostMapping("update-store-info")
    public Result<TbSeller> updateStoreInfo(@RequestBody TbSeller tbSeller) {
        tbSellerDao.update(tbSeller);
        return Result.ok(tbSeller);
    }

    @PostMapping("submit-store-info")
    public Result<TbSeller> submitStoreInfo(@RequestBody TbMessageAuditForm tbMessageAuditForm) {
        TbMessageAuditForm tbMessageAuditForm1 = tbMessageAuditFormDao.queryByStoreId(tbMessageAuditForm.getStoreId());
        tbMessageAuditForm.setAuditStatus(0);
        if (tbMessageAuditForm1 == null) {
            return Result.ok(tbMessageAuditFormDao.insert(tbMessageAuditForm));
        } else {
            tbMessageAuditForm.setAuditId(tbMessageAuditForm1.getAuditId());
            return Result.ok(tbMessageAuditFormDao.update(tbMessageAuditForm));
        }
    }
}
